package littlecyberwar.tool;

import javax.swing.*;
import java.awt.*;
import java.awt.geom.*;
import java.awt.event.*;
import java.util.*;
import littlecyberwar.model.*;

import littlecyberwar.ui.PlayingBoardPanel;
import littlecyberwar.ui.ScalingDelegate;

public class MouseRulerHandler extends MouseHandler {

	private static final int _AMOUNT_TO_GROW=70;	// need to erase old text 
  private Line2D.Double rulerLine;
  Font f = new Font("Serif", Font.BOLD, 14);
  String distToBeDisplayed;
  int strLocX, strLocY;
 

  public MouseRulerHandler() {
    super();
    // startDragX = -1;
    // startDragY = -1;
    id = H_RULER;
	unitSelectedText = "Drag mouse to end point to measure distance.";
	noUnitSelectedText = "Click on a spot on the map to begin ruler measurement";

  }

  public void startAction( MouseEvent e ) {
    startDragP = ScalingDelegate.getMouseEventLocation(e.getPoint());
    rulerLine = new Line2D.Double();
  }

  /**
   * override superclass impl
   * will always null the selected rectangle (the rubberbanding ruler)
   * as soon as the mouse is released.
   */
  public UnitCommand mouseUp( MouseEvent e ) {
	Rectangle r = null;
  	if ( selectedRectangle != null ) {
		r = selectedRectangle.getBounds();
  	}

    startDragP = null;
    selectedRectangle = null;
    selectedTrans = null;
    currTrans = null;
	distToBeDisplayed = null;
    modelChanged();
	
	if ( r != null ){
	     Rectangle scaledRec = ScalingDelegate.getScaledRectangle(r);
	     scaledRec.grow(_AMOUNT_TO_GROW, _AMOUNT_TO_GROW);
	      comp.repaint(scaledRec);
//	      comp.repaint(r);
	}
    return ( null );
  }

  public void mouseDragged( MouseEvent e ) {
    if ( startDragP != null ) {
      Rectangle oldR = rulerLine.getBounds();
      Point newP = ScalingDelegate.getMouseEventLocation(e.getPoint());
	  strLocX = newP.x;
	  strLocY = newP.y;
      rulerLine.setLine(startDragP, newP);

      // set selectedRectangle (a shape) so the line can be drawn
      selectedRectangle = rulerLine;
      int dist = (int)startDragP.distance(newP);
	  distToBeDisplayed = df.format(dist/mapScale);
      // comp.printToStatus("Distance measured= " + distToBeDisplayed + " " + mapDistanceUnit );
      //!! need to use bounds check to select just changed area
      Rectangle r = selectedRectangle.getBounds();
      r.add(oldR);
      Rectangle scaledRec = ScalingDelegate.getScaledRectangle(r);
      scaledRec.grow(_AMOUNT_TO_GROW, _AMOUNT_TO_GROW);
      comp.repaint(scaledRec);
//      comp.repaint(r);
    }
  }

  // for this tool there are no selected unit, so override this
  public boolean checkSelection( MouseEvent e ) {
    // always start action because only get here when mouse pressed
    startAction( e );
    return ( false );
  }

	public void paintStuff( Graphics2D g2 ){
		super.paintStuff(g2);
		// now draw the distance directly on to the board itself
		// need to reset the scaling to normal since we want the distance string to be the same
		// also need to adjust where the string is going to go
		//
		ScalingDelegate.resetScale(g2);
		int x = ScalingDelegate.getScaledDimension(strLocX+4);
		int y = ScalingDelegate.getScaledDimension(strLocY);
		
		if ( distToBeDisplayed != null ){
			g2.setFont(f);
			g2.drawString(distToBeDisplayed + " " + mapDistanceUnit, x, y);
		}
	}
}